<template>
	<view>
		<z-paging v-model="list" ref="paging" @query="queryList" show-refresher-when-reload>
			<uni-swipe-action ref="swipe_action">
				<uni-swipe-action-item :right-options="(isRoleAdmin() || type == 1) ? options : ''"
					v-for="(item,index) in list" :key="item._id" class="swipe_item_container"
					@click="onClick($event, item)">
					<uni-circle-item :circle="item"></uni-circle-item>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</z-paging>
		<uni-fab horizontal="right" v-if="isRoleAdmin() || type == 1" @click="circle_publish"></uni-fab>
	</view>
</template>

<script setup>
	import {
		onLoad
	} from '@dcloudio/uni-app'

	import {
		ref
	} from 'vue';

	import {
		isRoleAdmin
	} from '@/utils/role.js'

	const db = uniCloud.importObject("circleObj", {
		customUI: true
	});

	const type = ref(0)

	onLoad((params) => {
		if (params.type) {
			type.value = Number(params.type)
			uni.setNavigationBarTitle({
				title: '我的心得'
			})
		}
	})

	const options = ref([{
			text: "编辑",
			style: {
				backgroundColor: "#10ae18",
				fontSize: '13px'
			}
		},
		{
			text: "删除",
			style: {
				backgroundColor: '#E40C39',
				fontSize: '13px'
			}
		}
	])

	const list = ref([])
	const paging = ref(null)
	const swipe_action = ref(null)

	const queryList = async (pageNo, pageSize) => {
		let res = await db.getList(pageNo, pageSize, type.value)
		console.log("备考圈", res);
		if (res.errCode != 0) {
			paging.value.complete(false);
			return
		}
		paging.value.complete(res.data);
	}

	function onClick(e, item) {
		if (e.index == 0) {
			// 编辑
			circle_edit(item)
			swipe_action.value.closeAll()
		} else if (e.index == 1) {
			// 删除
			circle_delete_action(item)
		}
	}

	function circle_edit(params) {
		uni.navigateTo({
			url: '/pages/index/beikaoxinde/publish/publish?id=' + params._id
		})
	}

	function circle_publish() {
		uni.navigateTo({
			url: '/pages/index/beikaoxinde/publish/publish'
		})
	}

	/* 删除事件 */
	function circle_delete_action(params) {
		uni.showModal({
			title: '温馨提醒',
			content: `确定删除"${params.content}"?`,
			success: (res) => {
				if (!res.confirm) {
					swipe_action.value.closeAll()
					return
				}
				circle_delete(params)
			}
		})
	}
	/* 删除 */
	async function circle_delete(params) {
		uni.showLoading()
		let result = await db.circle_delete(params._id)
		uni.hideLoading()
		let flag = result.errCode == 0
		uni.showToast({
			title: flag ? "删除成功" : result.errMsg,
			icon: flag ? 'success' : 'fail'
		})
		if (flag) {
			paging.value.refresh()
		}
	}

	function circle_item_click(params) {
		uni.navigateTo({
			url: '/pages/index/beikaoxindexiangqing/beikaoxindexiangqing?id=' + params._id
		})
	}

	uni.$on('circle_is_like_emit', () => {
		if (!paging.value) {
			return
		}
		paging.value.reload()
	})

	uni.$on('circle_publish_success', () => {
		if (!paging.value) {
			return
		}
		paging.value.reload()
	})
</script>

<style lang="scss">
	page {
		background: $me-color-background;
	}
</style>

<style lang="scss" scoped>
	.swipe_item_container {}
</style>